﻿Imports System.IO
Imports System.Web

Public Class DT2PDF
    Public Shared Sub ExportToHTMLPDF(dataTable As DataTable, ByRef outputStream As Stream)
        'Dim sw As New StringWriter()
        'Dim w As New HtmlTextWriter(sw)

        'Dim tbl As New Table()

        ''Create column header row
        'Dim thr As New TableHeaderRow()
        'For Each col As DataColumn In dataTable.Columns
        '    Dim th As New TableHeaderCell()
        '    th.Text = col.Caption
        '    thr.Controls.Add(th)
        'Next
        'tbl.Controls.Add(thr)

        ''Create table rows
        'For Each row As DataRow In dataTable.Rows
        '    Dim tr As New TableRow()
        '    For Each value As Object In row.ItemArray
        '        Dim td As New TableCell()
        '        td.Text = value.ToString()
        '        tr.Controls.Add(td)
        '    Next
        '    tbl.Controls.Add(tr)
        'Next
        'tbl.RenderControl(w)

        'Dim idoc As New Document(PageSize.A4_LANDSCAPE)
        'Dim pdfw As PdfWriter = PdfWriter.GetInstance(idoc, outputStream)

        'idoc.Open()

        'Try
        '    Dim htmlContext As New HtmlPipelineContext(Nothing)
        '    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory())
        '    Dim cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(True)
        '    Dim pipe As New CssResolverPipeline(cssResolver, New HtmlPipeline(htmlContext, New PdfWriterPipeline(idoc, pdfw)))
        '    Dim worker As New XMLWorker(pipe, True)
        '    Dim p As New XMLParser(worker)

        '    p.Parse(New StringReader(sw.ToString()))
        'Catch ex As Exception

        'End Try
        'idoc.Close()
    End Sub

    Public Shared Sub ExportToPDF(dataTable As DataTable, ByRef outputStream As Stream)
        'Dim pdfDoc As New Document()
        'Try
        '    PdfWriter.GetInstance(pdfDoc, outputStream)
        '    pdfDoc.Open()
        '    Dim font8 As Font = FontFactory.GetFont("ARIAL", 7)
        '    Dim dt As DataTable = dataTable
        '    If (Not dt Is Nothing) Then
        '        Dim PdfTable As New PdfPTable(dt.Columns.Count)
        '        Dim PdfPCell As PdfPCell = Nothing
        '        For Each dColumn As DataColumn In dataTable.Columns
        '            PdfPCell = New PdfPCell(New Phrase(New Chunk(dColumn.ColumnName, font8)))
        '            PdfPCell.NoWrap = True
        '            PdfTable.AddCell(PdfPCell)
        '        Next
        '        For Each drRow As DataRow In dataTable.Rows
        '            For Each dColumn As DataColumn In dataTable.Columns
        '                PdfPCell = New PdfPCell(New Phrase(New Chunk(drRow(dColumn).ToString(), font8)))
        '                PdfPCell.NoWrap = True
        '                PdfPCell.Border = 0
        '                PdfTable.AddCell(PdfPCell)
        '            Next
        '        Next
        '        pdfDoc.Add(PdfTable)
        '    End If
        '    pdfDoc.Close()
        'Catch ex As Exception
        '    Throw ex
        'End Try
    End Sub
End Class
